package com.yylany.controller;

import com.yylany.emunt.CommonConstants;
import com.yylany.mapper.CommunityMapper;
import com.yylany.mapper.HouseMapper;
import com.yylany.pojo.Comment;
import com.yylany.pojo.House;
import com.yylany.pojo.HouseUser;
import com.yylany.result.ResultMsg;
import com.yylany.service.AgencyService;
import com.yylany.service.CommentService;
import com.yylany.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
public class HouseController {


    @Autowired
    private CommunityMapper communityMapper;

    @Autowired
    private HouseMapper houseMapper;

    @Autowired
    private HouseService houseService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private AgencyService agencyService;

    /**
     * 1.实现分页
     * 2.支持小区搜索、类型搜索
     * 3.支持排序
     * 4.支持展示图片、价格、标题、地址等信息
     *
     * @return
     */
    @RequestMapping("/house/list")
    public String houseList(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                            House query, ModelMap modelMap) {
        List<House> ps = houseService.queryHouse(query, pageSize, pageNum);
        List<House> rcHouses = houseService.getHotHouse(CommonConstants.RECOM_SIZE);
        modelMap.put("recomHouses", rcHouses);
        modelMap.put("vo", query);
        modelMap.put("ps", ps);

        return "house/listing";
    }

    @RequestMapping("/house/toAdd")
    public String toAdd(ModelMap modelMap) {
        modelMap.put("citys", communityMapper.selectAll());
        modelMap.put("communitys", communityMapper.selectAll());
        return "/house/add";
    }

    @RequestMapping("/house/add")
    public String doAdd(House house) {

        houseMapper.insert(house);

        return "redirect:/house/ownlist";
    }

    @RequestMapping("house/ownlist")
    public String ownlist(House house, Integer pageNum, Integer pageSize, ModelMap modelMap) {

        return "/house/ownlist";
    }

    /**
     * 查询房屋详情
     * 查询关联经纪人
     *
     * @param id
     * @return
     */
    @RequestMapping("house/detail")
    public String houseDetail(Long id, ModelMap modelMap) {

        House house = houseService.queryOneHouse(id);
        HouseUser houseUser = houseService.getHouseUser(id);
        List<Comment> comments = commentService.getHouseComments(id, 8);
        if (houseUser.getUserId() != null && !houseUser.getUserId().equals(0)) {
            modelMap.put("agent", agencyService.getAgentDeail(houseUser.getUserId()));
        }
        List<House> rcHouses = houseService.getHotHouse(CommonConstants.RECOM_SIZE);
        modelMap.put("recomHouses", rcHouses);
        modelMap.put("house", house);
        modelMap.put("commentList", comments);


        return "/house/detail";
    }

//    @RequestMapping("house/leaveMsg")
//    public String houseMsg(UserMsg userMsg) {
//
////	  return "redirect:/house/detail?id=" + userMsg.getHouseId() + ResultMsg.successMsg("留言成功").asUrlParams();
//        return "";
//
//    }


    //1.评分
    @ResponseBody
    @RequestMapping("house/rating")
    public ResultMsg houseRate(Double rating, Long id) {
        return ResultMsg.successMsg("ok");
    }


    //2.收藏
    @ResponseBody
    @RequestMapping("house/bookmark")
    public ResultMsg bookmark(Long id) {

        return ResultMsg.successMsg("ok");
    }

    //3.删除收藏
    @ResponseBody
    @RequestMapping("house/unbookmark")
    public ResultMsg unbookmark(Long id) {

        return ResultMsg.successMsg("ok");
    }

    @RequestMapping(value = "house/del")
    public String delsale(Long id, String pageType) {

        return "redirect:/house/ownlist";
    }

    //4.收藏列表
    @RequestMapping("house/bookmarked")
    public String bookmarked(House house, Integer pageNum, Integer pageSize, ModelMap modelMap) {

        return "/house/ownlist";
    }
}
